package org.lagou.servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.lagou.Dao.TypeDao;
import org.lagou.Dao.impl.TypeDaoImpl;
import org.lagou.bean.ReponseBody;

import com.google.gson.Gson;

/**
 * Servlet implementation class TypeServlet
 */
@WebServlet("/TypeServlet")
public class TypeServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	TypeDao dao=new TypeDaoImpl();
	Gson gson = new Gson();
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		String type = request.getParameter("type");
		switch (type) {
		case "getTypeFirst":
			getTypeFirst(request,response);
			break;
		case "getTypeSecond":
			getTypeSecond(request,response);
			break;
		case "getTypeAll":
			getTypeAll(request,response);
			break;	

		default:
			break;
		}
	}

	
	private void getTypeAll(HttpServletRequest request, HttpServletResponse response) {
		ReponseBody rb=new ReponseBody();
		rb.setData(dao.getTypeAll());
		try {
			response.getWriter().write(gson.toJson(rb));
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}		
	}

	private void getTypeSecond(HttpServletRequest request, HttpServletResponse response) {
		ReponseBody rb = new ReponseBody();
		int superiorid = Integer.parseInt(request.getParameter("superiorid"));
		rb.setData(dao.getTypeSecond(superiorid));
		try {
			response.getWriter().write(gson.toJson(rb));
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	

	private void getTypeFirst(HttpServletRequest request, HttpServletResponse response) {
		ReponseBody rb = new ReponseBody();
		int superiorid = Integer.parseInt(request.getParameter("superiorid"));
		int show1 = Integer.parseInt(request.getParameter("show1"));
		rb.setData(dao.getTypeFirst(superiorid, show1));
		try {
			response.getWriter().write(gson.toJson(rb));
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
